<template>
  <div class="Maintenance">
    <!-- header -->
    <div class="header">
      <h2>维保取车登记</h2>
      <div class="buttons">
        <el-button
          type="primary"
          icon="el-icon-circle-plus-outline"
          @click="add"
          >新增</el-button
        >
        <el-button type="primary" icon="el-icon-share"></el-button>
        <el-button type="primary" icon="el-icon-delete"></el-button>
        <el-button type="primary" icon="el-icon-search">搜索</el-button>
        <el-button type="primary"
          >上传<i class="el-icon-upload el-icon--right"></i
        ></el-button>
      </div>
    </div>
    <!-- header -->
    <!--  -->
    <!-- table -->
    <el-table
      :data="
        tableData.filter(
          (data) =>
            !search ||
            data.carNumber.toLowerCase().includes(search.toLowerCase())
        )
      "
      style="width: 100%"
      height="650"
      border
    >
      <el-table-column type="selection" width="50"> </el-table-column>
      <el-table-column prop="id" label="取车登记号" width="180" sortable>
      </el-table-column>
      <el-table-column prop="wbId" label="维保登记单号" width="180" sortable>
      </el-table-column>
      <el-table-column prop="carNumber" label="维保车辆" width="165" sortable>
      </el-table-column>
      <el-table-column prop="pickDate" label="取车日期" width="165" sortable>
        <template slot-scope="scope">
          <span>{{ scope.row.wbDate | dateFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="wbPirce" label="维保费用" width="165">
      </el-table-column>
      <el-table-column prop="carRunnning" label="行驶里程" width="165">
      </el-table-column>
      <el-table-column prop="carState" label="车辆状态" min-width="165">
      </el-table-column>
      <el-table-column fixed="right" width="165">
        <template slot="header" slot-scope="">
          <el-input v-model="search" size="mini" placeholder="输入关键字搜索" />
        </template>
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small">
            查看
          </el-button>
          <el-button type="text" size="small" @click="change(scope.row)"
            >编辑</el-button
          >
          <el-button
            type="text"
            size="small"
            style="color: red"
            @click="del(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- table -->
    <!--  -->
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        @current-change="current_change"
        background
        layout="prev, pager, next"
        :total="blockall"
      >
      </el-pagination>
    </div>
    <!-- 分页 -->
    <!--  -->
    <!-- 详情 -->
    <el-drawer
      title="详情"
      :visible.sync="drawer"
      :direction="direction"
      :before-close="cancel"
      :size="500"
    >
      <div class="details">
        <div class="details-main">
          <el-form label-position="top" label-width="80px">
            <el-form-item label="取车登记号">
              {{ details.id }}
            </el-form-item>
            <el-form-item label="维保登记单号">
              {{ details.wbId }}
            </el-form-item>
            <el-form-item label="维保车辆">
              {{ details.carNumber }}
            </el-form-item>
            <el-form-item label="日期">
              {{ details.pickDate | dateFilter }}
            </el-form-item>
            <el-form-item label="维保费用">
              {{ details.wbPirce }}
            </el-form-item>
            <el-form-item label="行驶里程">
              {{ details.carRunnning }}
            </el-form-item>
            <el-form-item label="车辆状态">
              {{ details.carState }}
            </el-form-item>
          </el-form>
          <!-- 表单 -->
        </div>
        <div class="drawer__footer">
          <el-button type="primary" @click="cancel"> 确 定 </el-button>
        </div>
      </div>
    </el-drawer>
    <!-- 详情 -->
    <!--  -->
    <!-- 添加 -->
    <el-dialog title="新增" :visible.sync="dialogAddVisible">
      <div class="add">
        <el-form
          ref="addform"
          :model="addform"
          label-width="auto"
          :rules="addformrules"
        >
          <el-form-item label="维保登记单号" label-width="auto" prop="wbId">
            <el-select v-model="addform.wbId" placeholder="请选择">
              <el-option
                v-for="(v, index) in wblist"
                :label="v.wbId"
                :value="v.wbId"
                :key="index"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="维保车辆" prop="carNumber">
            <el-select v-model="addform.carNumber" placeholder="请选择">
              <el-option label="京B168465" value="京B168465"></el-option>
              <el-option label="陕A156834" value="陕A156834"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="取车日期" prop="pickDate">
            <el-col :span="11">
              <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="addform.pickDate"
                style="width: 100%"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-col>
          </el-form-item>
          <el-form-item label="维保费用" prop="wbPirce">
            <el-input v-model="addform.wbPirce"></el-input>
          </el-form-item>
          <el-form-item label="行驶里程" prop="carRunnning">
            <el-input v-model="addform.carRunnning"></el-input>
          </el-form-item>
          <el-form-item label="车辆状态" prop="carState">
            <el-radio-group v-model="addform.carState">
              <el-radio label="可用" name="正常维修"></el-radio>
              <el-radio label="维修" name="维修"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onAddSubmit(addform)"
              >提交</el-button
            >
            <el-button @click="dialogAddVisible = false">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
    <!-- 添加 -->
    <!--  -->
    <!-- 编辑 -->
    <el-dialog title="编辑" :visible.sync="dialogChangeVisible">
      <div class="change">
        <el-form ref="details" :model="details" label-width="80px">
          <el-form-item label="取车登记号" label-width="auto">
            {{ details.id }}
          </el-form-item>
          <el-form-item label="维保登记单号" label-width="auto">
            <el-select v-model="details.wbId" placeholder="请选择">
              {{ details.wbId }}
              <el-option
                v-for="(v, index) in wblist"
                :label="v.wbId"
                :value="v.wbId"
                :key="index"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="维保车辆">
            <el-select v-model="details.carNumber" placeholder="请选择">
              <el-option label="京B168465" value="京B168465"></el-option>
              <el-option label="陕A156834" value="陕A156834"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="取车日期">
            <el-col :span="11">
              <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="details.pickDate"
                style="width: 100%"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-col>
          </el-form-item>
          <el-form-item label="维保费用">
            <el-input v-model="details.wbPirce"></el-input>
          </el-form-item>
          <el-form-item label="行驶里程">
            <el-input v-model="details.carRunnning"></el-input>
          </el-form-item>
          <el-form-item label="车辆状态">
            <el-radio-group v-model="details.carState">
              <el-radio label="可用" name="正常维修"></el-radio>
              <el-radio label="维修" name="维修"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onChangeSubmit(details)"
              >提交</el-button
            >
            <el-button @click="dialogChangeVisible = false">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
    <!-- 编辑 -->
  </div>
</template>

<script>
import {
  javapicklist,
  javaaddpick,
  javadelpick,
  javagetpick,
} from "../../apis/PickupCar";
import { javawblist } from "../../apis/Maintenance";
export default {
  filters: {
    dateFilter(val) {
      if (val) {
        return val.slice(0, 10);
      }
    },
  },
  components: {},
  mounted() {
    //获取json
    //总页数
    // wbAllList().then((ok) => {
    //   console.log(ok.data.length);
    //   this.blockall = ok.data.length;
    // });
    //分页查询
    // wbList().then((ok) => {
    //   this.tableData = ok.data;
    // });

    //获取后台
    javapicklist().then((ok) => {
      console.log(ok);
      this.tableData = ok.data.data.records;
      this.blockall = ok.data.data.total;
    });
    //维保列表
    javawblist(1, 20).then((ok) => {
      console.log(ok);
      this.wblist = ok.data.data.records;
    });
  },
  methods: {
    //页码
    current_change(i) {
      console.log(i);
      this.index = i;
      //分页查询
      javapicklist(i).then((ok) => {
        console.log(ok);
        this.tableData = ok.data.data.records;
      });
    },
    //编辑
    change(row) {
      this.details = row;
      this.dialogChangeVisible = true;
    },
    //编辑提交
    onChangeSubmit(obj) {
      console.log("新增", obj);
      if (this.dialogChangeVisible) {
        javaaddpick(obj).then((ok) => {
          console.log(ok);
          if (ok.data.code === 200) {
            this.dialogChangeVisible = false;
          }
        });
      }
    },
    //新增按钮
    add() {
      this.dialogAddVisible = true;
    },
    //随机数
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    },
    //随机id
    randomID() {
      let reID = "";
      for (let i = 0; i < 6; i++) {
        reID += this.randomNum(0, 9);
      }
      return reID / 1;
    },
    //新增提交
    onAddSubmit(obj) {
      console.log(obj);
      this.$refs["addform"].validate((valid) => {
        if (valid) {
          javaaddpick(obj).then((ok) => {
            console.log(ok);
            if (ok.data.code === 200) {
              //添加页面初始化
              this.$refs["addform"].resetFields();
              this.dialogAddVisible = false;
              // 获取列表
              javapicklist(this.index).then((ok) => {
                console.log(ok);
                this.tableData = ok.data.data.records;
                this.blockall = ok.data.data.total;
              });
            }
          });
          // obj.wbId = this.randomID();
          // addwb(obj).then((ok) => {
          //   console.log(ok);
          //   if (ok.status === 201) {
          //     this.$refs["addform"].resetFields();
          //     this.dialogAddVisible = false;
          //     //总页数
          //     wbAllList().then((ok) => {
          //       this.blockall = ok.data.length;
          //     });
          //     //当前页
          //     wbList(this.index).then((ok) => {
          //       this.tableData = ok.data;
          //     });
          //   }
          // });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //删除
    del(row) {
      // console.log(row);
      javadelpick(row).then((ok) => {
        console.log(ok);
        javapicklist(this.index).then((ok) => {
          // console.log(ok.data.data.records);
          if (ok.data.data.records == 0) {
            this.index--;
            javapicklist(this.index).then((ok) => {
              this.tableData = ok.data.data.records;
              this.blockall = ok.data.data.total;
            });
          } else {
            javapicklist(this.index).then((ok) => {
              this.tableData = ok.data.data.records;
              this.blockall = ok.data.data.total;
            });
          }
        });
        //总页数
        // wbAllList().then((ok) => {
        //   this.blockall = ok.data.length;
        // });
        //当前页
        // wbList(this.index).then((ok) => {
        //   console.log(ok.data.length);
        //   if (ok.data.length == 0) {
        //     this.index--;
        //     wbList(this.index).then((ok) => {
        //       this.tableData = ok.data;
        //     });
        //   } else {
        //     wbList(this.index).then((ok) => {
        //       this.tableData = ok.data;
        //     });
        //   }
        // });
      });
    },
    //详情取消
    cancel() {
      this.drawer = false;
    },
    // 关闭详情抽屉
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(() => {
          done();
        })
        .catch(() => {});
    },
    //查看
    handleClick(row) {
      console.log(row);
      javagetpick(row).then((ok) => {
        console.log(ok);
        if (ok.data.code === 200) {
          this.details = ok.data.data;
          this.drawer = !this.drawer;
        }
      });
    },
  },
  data() {
    return {
      //维保列表
      wblist: [],
      //当前页码
      index: 1,
      //分页总数
      blockall: 1,
      // 编辑对话框
      dialogChangeVisible: false,
      //添加对话框
      dialogAddVisible: false,
      //抽屉
      direction: "rtl",
      drawer: false,
      //搜索
      search: "",
      //列表
      tableData: [],
      //详情
      details: {},
      //添加表单
      addform: {
        carNumber: "",
        carRunnning: "",
        carState: "",
        pickDate: "",
        wbId: "",
        wbPirce: "",
        id: "",
      },
      addformrules: {
        carNumber: [
          { required: true, message: "请选择车辆", trigger: "change" },
        ],
        wbId: [
          { required: true, message: "请选择维保单号", trigger: "change" },
        ],
        carRunnning: [
          { required: true, message: "请填写行驶里程", trigger: "change" },
        ],
        carState: [
          { required: true, message: "请选择车辆状态", trigger: "change" },
        ],
        pickDate: [
          {
            type: "string",
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
        wbPirce: [
          { required: true, message: "请填写维保费用", trigger: "change" },
        ],
      },
    };
  },
};
</script>

<style lang="scss" scoped>
.Maintenance {
  // height: 100%;
  // width: auto;
  // min-width: 444px;
  min-height: 800px;
  background-color: white;
  padding: 10px;
  box-sizing: border-box;
  border-radius: 5px;
  position: relative;

  //头部
  .header {
    h2 {
      margin: 5px 0;
      font-size: 18px;
      font-weight: 400;
    }
    .buttons {
      margin: 20px 0;
    }
  }
  //详情
  .details {
    height: 100%;
    display: flex;
    flex-direction: column;
    .details-main {
      margin-top: 50px;
      padding: 20px;
      flex: 1;
    }
    .drawer__footer {
      display: flex;
      justify-content: center;
    }
  }
  //分页
  .block {
    margin: 10px 0;
  }
}
</style>